$(function(){
	var j = {
		config: {
			times: 3000
		},
		msg: {
			success: '报名成功',
			miss: '未填写哦~'
		}
	};
	j.alertPop = function(msg, time){
		var self = this;
		if($('.pop-alert')[0]){
			$('.pop-alert').html(msg);
		}else{
			$('body').append('<div class="pop-alert">'+ msg +'</div>');
			var $alert = $('.pop-alert');
			$alert.css({left:'50%','margin-left': -$alert.outerWidth()/2, bottom: '20%'});
			setTimeout(function(){
				$alert.remove();
			},time || j.config.times);
		}
	};
	var cansubmit = true;
	$('#submit').click(function(){
		var data = getData();
		if(cansubmit){
			$("#form").submit();
		}
	});
	function getData(){
		var data = {};
		$('.data').each(function(){
			var $this = $(this),
				type = $this.attr('type'),
				name = $this.attr('name'),
				val = $.trim($this.val()),
				must = $this.attr('must'),
				title = $this.attr('title');
			switch(type){
				case 'text':
					if(must == 1 && val == ''){
						j.alertPop(title + j.msg.miss);
						$('html,body').animate({scrollTop: $this.offset().top}, 'normal');
						cansubmit = false;
						return false;
					}else{
						data[name] = val;
						cansubmit = true;
					}
					break;
				case 'radio':
					if($this.is(':checked')){
						data[name] = val;
					}
					break;
				case 'select':
					data[name] = val;
					break;
				case 'checkbox':
					if($this.is(':checked')){
						data[name] = val;
						cansubmit = true;
					}else{
						j.alertPop(title + '未接受~');
						cansubmit = false;
						return false;
					}
					break;
				case 'file':
					data[name] = document.getElementById("doc").files;
					break;
			}
		});
		return data;
	}
	$('#doc').change(function(){
		setImagePreview();
	});

	function setImagePreview(avalue) {
		var docObj = document.getElementById("doc");
		var imgObjPreview = document.getElementById("preview");
		if (docObj.files && docObj.files[0]) {
			//火狐下，直接设img属性
			if(docObj.files[0].getAsDataURL){
				imgObjPreview.src = docObj.files[0].getAsDataURL();
			}else{
				//火狐7以上
				imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
			}
		} else {
			//IE下，使用滤镜
			docObj.select();
			var imgSrc = document.selection.createRange().text;
			var localImagId = document.getElementById("localImag");
			//必须设置初始大小
			localImagId.style.width = "150px";
			localImagId.style.height = "180px";
			//图片异常的捕捉，防止用户修改后缀来伪造图片
			try {
				localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
				localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
			} catch(e) {
				alert("您上传的图片格式不正确，请重新选择!");
				return false;
			}
			imgObjPreview.style.display = 'none';
			document.selection.empty();
		}
		return true;
	}


});